﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BuySoftwareOnline.Domain.Abstract;
using BuySoftwareOnline.Web.Models;
using BuySoftwareOnline.Domain;

namespace BuySoftwareOnline.Web.Controllers
{
    public class ProductController : Controller
    {
        //
        // GET: /Product/
        public int PageSize = 4;
        private IProductRepository repository;

        public ProductController(IProductRepository productRepository)
        {
            repository = productRepository;
        }

        public ViewResult List(string category,int page = 1)
        {
            ProductsListViewModel viewModel = new ProductsListViewModel
            {
                Products = repository.Products
                .Where(p => category ==null || p.Category.CategoryName == category)
                .OrderBy(p => p.ProductID)
                .Skip((page - 1) * PageSize)
                .Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = repository.Products.Where(p => category == null || p.Category.CategoryName == category).Count()
                },
                CurrentCategory = category
            };
            return View(viewModel);
            
        }

        public FileContentResult GetImage(string productId)
        {
            Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId);
            if (prod != null)
            {
                return File(prod.ImageContent,prod.ImageType);
            }
            else
            {
                return null;
            }
        }

        public FileContentResult GetScreenShot(string productId)
        {
            Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId);
            if (prod != null)
            {
                return File(prod.ScreenShot, prod.ScreenShotType);
            }
            else
            {
                return null;
            }
        }

        public ActionResult Index()
        {
            return View();
        }

    }
}
